package com.example.wzh.robotsdemo;

import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;

import com.efrobot.library.speechsdk.SpeechSdkProvider;
import com.example.wzh.robotsdemo.controlbean.AirBean;
import com.example.wzh.robotsdemo.controlbean.GoHomeBean;
import com.example.wzh.robotsdemo.controlbean.HumidityBean;
import com.example.wzh.robotsdemo.controlbean.LightBean;
import com.example.wzh.robotsdemo.controlbean.OutHomeBean;
import com.example.wzh.robotsdemo.controlbean.SaftyBean;
import com.example.wzh.robotsdemo.controlbean.curtainBean;

import java.io.IOException;

import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

/**
 * Created by WZH on 2017/9/6.
 */

public class MyContentProvider extends SpeechSdkProvider {

    private static final String TAG = "CusProvider";
    public static final MediaType JSON
            = MediaType.parse("application/json; charset=utf-8");
    private OkHttpClient client;
    private SharedPreferences preferences;
    private String url;
    private String token;

    @Override
    public void onReceiveMessage(String code, String speechContent) {

        //从内存中获取数据
        Context context = getContext();
        preferences = context.getSharedPreferences("sp_file", Context.MODE_PRIVATE);
        token=preferences.getString("token",null);
        Log.e(TAG,"token===" + token);

        int selecttype = preferences.getInt("selecttype", -1);
        Log.e(TAG,"selecttype===" + selecttype);

        if(selecttype == 0) {
            //选择了合肥样板间1
            url =LoginUtils.K_URL;
        }else if(selecttype == 1) {
            url =LoginUtils.K_URL2;
        }else if(selecttype == 2) {
            url =LoginUtils.K_URL3;
        }
        else if(selecttype == 3) {
            url =LoginUtils.K_URL4;
        }
        else if(selecttype == 4) {
            url =LoginUtils.K_URL5;
        }
        else if(selecttype == 5) {
            url =LoginUtils.K_URL6;
        }

        Log.i(TAG, "MyContentProviser  receive=" + code + " speechContent=" + speechContent + " Thread=" + Thread.currentThread().getName());
        //在这里执行命令，连接服务器
        client = new OkHttpClient();
//        Log.e(TAG,""+"1000".equals(code));
        //拉开窗帘
        if ("1000".equals(code)) {
            try {
                curtainBean kbean = new curtainBean(1, 1);
                post(url, kbean.toString());
                // postData();
                //  SpeechManager.getInstance().closeSpeechDiscern(this.getContext());
                // SpeechManager.getInstance().removeSpeechState(this.getContext(), 11);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }else if("1001".equals(code)) {
            try {
                curtainBean kbean = new curtainBean(1,1);
                post(url,kbean.toString());
                // postData();
                //  SpeechManager.getInstance().closeSpeechDiscern(this.getContext());
                // SpeechManager.getInstance().removeSpeechState(this.getContext(), 11);
            } catch (Exception e) {
                e.printStackTrace();
            } {
        }
            //把窗帘拉上
        }else if ("1002".equals(code)) {
            try {
                curtainBean kbean = new curtainBean(0, 1);
                post(url, kbean.toString());
//                SpeechManager.getInstance().openSpeechDiscern(this.getContext());
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else if ("1003".equals(code)) {
                try {
                    curtainBean kbean = new curtainBean(0,1);
                    post(url, kbean.toString());
//                SpeechManager.getInstance().openSpeechDiscern(this.getContext());
                } catch (Exception e) {
                    e.printStackTrace();
                }
            //打开空气净化
        }  else if ("1004".equals(code)) {
            try {
                AirBean kbean = new AirBean(1,1);
                post(url, kbean.toString());
//                SpeechManager.getInstance().openSpeechDiscern(this.getContext());
            } catch (Exception e) {
                e.printStackTrace();
            }
    }  else if ("1005".equals(code)) {
        try {
            AirBean kbean = new AirBean(1,1);
            post(url, kbean.toString());
//                SpeechManager.getInstance().openSpeechDiscern(this.getContext());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    //关闭空气净化
        else if ("1006".equals(code)) {
            try {
                AirBean kbean = new AirBean(0,1);
                post(url, kbean.toString());
//                SpeechManager.getInstance().openSpeechDiscern(this.getContext());
            } catch (Exception e) {
                e.printStackTrace();
            }
            //开灯
        }else if ("1007".equals(code)) {
            try {
                LightBean bean = new LightBean(1,1);
                post(url, bean.toString());
//                SpeechManager.getInstance().openSpeechDiscern(this.getContext());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }else if ("1008".equals(code)) {
            try {
                LightBean bean = new LightBean(1,1);
                post(url, bean.toString());
//                SpeechManager.getInstance().openSpeechDiscern(this.getContext());
            } catch (Exception e) {
                e.printStackTrace();
            }
//        }else if ("1009".equals(code)) {
//            try {
//                LightBean bean = new LightBean(1,1);
//                post(url, bean.toString());
////                SpeechManager.getInstance().openSpeechDiscern(this.getContext());
//            } catch (Exception e) {
//                e.printStackTrace();
//            }
            //关灯
        }
        else if ("1010".equals(code)) {
            try {
                LightBean bean = new LightBean(0, 1);
                post(url, bean.toString());
//                SpeechManager.getInstance().openSpeechDiscern(this.getContext());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
            else if ("1011".equals(code)) {
            try {
                LightBean bean = new LightBean(0, 1);
                post(url, bean.toString());
//                SpeechManager.getInstance().openSpeechDiscern(this.getContext());
            } catch (Exception e) {
                e.printStackTrace();
            }
//        }
//            else if ("1012".equals(code)) {
//                try {
//                    LightBean bean = new LightBean(0,1);
//                    post(url, bean.toString());
////                SpeechManager.getInstance().openSpeechDiscern(this.getContext());
//                } catch (Exception e) {
//                    e.printStackTrace();
//                }
            //开启布防
        }else if ("1013".equals(code)) {
            try {
                SaftyBean kbean = new SaftyBean(1);
                post(url, kbean.toString());
//                SpeechManager.getInstance().openSpeechDiscern(this.getContext());
            } catch (Exception e) {
                e.printStackTrace();
            }
            //关闭布防
        }else if ("1014".equals(code)) {
            try {
                SaftyBean kbean = new SaftyBean(0);
                post(url, kbean.toString());
//                SpeechManager.getInstance().openSpeechDiscern(this.getContext());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        //回家
        else if ("1015".equals(code)) {
            try {
                GoHomeBean kbean = new GoHomeBean(1,1);
                post(url, kbean.toString());
//                SpeechManager.getInstance().openSpeechDiscern(this.getContext());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    //芝麻开门
    else if ("1016".equals(code)) {
            try {
                GoHomeBean kbean = new GoHomeBean(1, 1);
                post(url, kbean.toString());
//                SpeechManager.getInstance().openSpeechDiscern(this.getContext());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        //外出
        else if ("1017".equals(code)) {
            try {
                OutHomeBean kbean = new OutHomeBean(1,1);
                post(url, kbean.toString());
//                SpeechManager.getInstance().openSpeechDiscern(this.getContext());
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else if ("1018".equals(code)) {
            try {
                OutHomeBean kbean = new OutHomeBean(1, 1);
                post(url, kbean.toString());
//                SpeechManager.getInstance().openSpeechDiscern(this.getContext());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }//打开加湿器
        else if ("1019".equals(code)) {
            try {
                HumidityBean kbean = new HumidityBean(1, 1);
                post(url, kbean.toString());
//                SpeechManager.getInstance().openSpeechDiscern(this.getContext());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }//关闭加湿器
        else if ("1020".equals(code)) {
            try {
                HumidityBean kbean = new HumidityBean(0, 1);
                post(url, kbean.toString());
//                SpeechManager.getInstance().openSpeechDiscern(this.getContext());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        else if("1021".equals(code)) {
            Log.e(TAG,"介绍公司");
        }
    }
    //联网请求发送命令控制
    String post(String url, String json) throws IOException {

        Log.e(TAG,"url====" + url);

        RequestBody body = RequestBody.create(JSON,json);
        Request request = new Request.Builder()
                .url(url)
                .post(body)
                .addHeader("Accept", LoginUtils.ACCEPT)
                .addHeader("X-Gizwits-Application-Id", LoginUtils.APPID)
                .addHeader("X-Gizwits-User-token",token)
                .build();
        Response response = client.newCall(request).execute();
        Log.e(TAG,"okhttp-----" + request);
        return response.body().string();
    }



//    public void postData(){
//       // Log.e(TAG,"KBEAN=====" + kbean);
////        String did = SpUtils.getString(getContext(), "did");
//
//        String str =  "{\"attrs\" : {" +
//                "\"event_curtain_control\":" + 1  +
//                ",\"event_curtain_control_helper\":" + 1 +
//                "}}";
//        OkHttpUtils
//                .postString()
//                .url(url)
//                .content(new Gson().toJson(kbean))
//                .addHeader("Accept", LoginUtils.ACCEPT)
//                .addHeader("X-Gizwits-Application-Id", LoginUtils.APPID)
//                .addHeader("X-Gizwits-User-token",SpUtils.getString(getContext(),"token"))
//                .mediaType(MediaType.parse("application/json; charset=utf-8"))
//                .build()
//                .execute(new StringCallback() {
//                    @Override
//                    public void onError(Call call, Exception e, int id) {
//                        Log.e(TAG,"请求指令的联网失败" + e.getMessage());
//                        Log.e(TAG,"请求指令的联网失败" + e.getCause());
//                        Log.e(TAG,"请求指令的联网失败" + e.toString());
//                    }
//                    @Override
//                    public void onResponse(String response, int id) {
//                        Log.e(TAG,"请求指令的联网成功" + response);
//                    }
//                });
//    }

    @Override
    public void TTSEnd() {
        super.TTSEnd();
        Log.i(TAG, "TTSEnd");
    }

    @Override
    public void hitHead(String s) {
        super.hitHead(s);
        Log.i(TAG, "当前头顶按钮状态： " + s);
    }

}
